package com.hzdl.web;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: Sfx
 * Date: 2018/9/3 0003
 * Time: 下午 5:11
 * To change this template use File | Settings | File Templates.
 */
public class Intercaptor1 implements HandlerInterceptor {
    private String[] ignoreList = {"/user/toLogin", "/user/login"};
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        String uri = req.getRequestURI();
        HttpSession session = req.getSession();
        if(session.getAttribute("user")==null){
            boolean flag = false;
            for (String s : ignoreList) {
                if(uri.contains(s)){
                    return true;
                }

            }
           if (!flag){
               req.setAttribute("msg", "请先登录再访问!");
               req.getRequestDispatcher("/user/toLogin").forward(req, resp);
               return false;
           }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
